package cn.edu.zut.internship.service.impl.company;

import cn.edu.zut.internship.bean.sys.Result;
import cn.edu.zut.internship.bean.sys.SysUser;
import cn.edu.zut.internship.iservice.company.BindingSV;
import cn.edu.zut.internship.iservice.notify.NotifySV;
import cn.edu.zut.internship.mapper.company.BindingHistoryMapper;
import cn.edu.zut.internship.mapper.company.BindingMapper;
import cn.edu.zut.internship.mapper.setting.SettingMapper;
import cn.edu.zut.internship.service.impl.result.ResultSVImpl;
import cn.edu.zut.internship.utils.ExcelUtils;
import cn.edu.zut.internship.utils.FileNameUtil;
import cn.edu.zut.internship.utils.PrimaryKeyUtil;
import cn.edu.zut.internship.utils.ThreadPoolUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.regexp.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BindingSVImpl implements BindingSV {
    @Autowired
    BindingMapper bindingMapper;
    @Autowired
    BindingHistoryMapper bindingHistoryMapper;
    @Autowired
    PrimaryKeyUtil primaryKeyUtil;
    @Autowired
    NotifySV notifySV;
    @Autowired
    SettingMapper settingMapper;
    @Override
    public Result selectBinding(Map params) {
        Result result = new Result();
        //获取当前学期
        Map semester = settingMapper.getSetting("currentSemester");
        String nowSemester = (String) semester.get("settingValue");
        params.put("semester",nowSemester);
        SysUser userInfo = (SysUser) params.get("userInfo");
        if(userInfo.getUserType()==1){
            params.put("teachAccount",userInfo.getUserAccount());
        }
        List<Map> bindingList = bindingMapper.selectBinding(params);
        int total = bindingMapper.selectBindingCount(params);
        result.setBeans(bindingList);
        result.getBean().put("total",total);
        return result;
    }

    @Override
    public Result getCompanyHistory(Map params) {
        Result result = new Result();
        List<Map> companyHistory = bindingHistoryMapper.getCompanyHistory(params);
        result.setBeans(companyHistory);
        return result;
    }

    @Override
    public Result cancelCompany(String studentAccount) {
        Result result = new Result();
        //查询绑定关系
        Map binding = bindingMapper.getBinding(studentAccount);
        //存入实训单位绑定历史表
        Long siHistoryId = primaryKeyUtil.generatePrimaryKey("student_internship_history");
        Map history = new HashMap();
        history.put("siHistoryId",siHistoryId);
        history.put("studentAccount",binding.get("studentAccount"));
        history.put("internshipId",binding.get("internshipId"));
        history.put("internshipName",binding.get("internshipName"));
        history.put("internshipAddress",binding.get("internshipAddress"));
        history.put("internshipType",binding.get("internshipType"));
        history.put("tutor",binding.get("tutor"));
        history.put("tutorPhone",binding.get("tutorPhone"));
        history.put("content",binding.get("content"));
        history.put("contract",binding.get("contract"));
        history.put("photo",binding.get("photo"));
        history.put("startTime",binding.get("createTime"));
        bindingHistoryMapper.instertHistory(history);
        //删除绑定关系
        bindingMapper.cancelCompany(studentAccount);
        return result;
    }

    @Override
    public Result exportBinding(Map params) {
        Result result = new Result();
        ThreadPoolUtil.getThreadPool().execute(new task(params));
        result.setMessage("后台正在导出");
        return result;
    }

    @Override
    public Result getStudentCompany(Map params) {
        Result result = new Result();
        Map studentCompany = bindingMapper.getStudentCompany(params);
        result.setBean(studentCompany);
        return result;
    }

    @Override
    public Result passStudentCompany(Map params) {
        Result result = new Result();
        SysUser userInfo = (SysUser) params.get("userInfo");
        params.put("teachAccount",userInfo.getUserAccount());
        params.put("internshipStatus",1);
        bindingMapper.updateStudentCompany(params);
        return result;
    }

    @Override
    public Result rejectStudentCompany(Map params) {
        Result result = new Result();
        SysUser userInfo = (SysUser) params.get("userInfo");
        params.put("teachAccount",userInfo.getUserAccount());
        params.put("internshipStatus",2);
        bindingMapper.updateStudentCompany(params);
        return result;
    }

    @Override
    public Result testBinding(Map params) {
        Result result = new Result();
        SysUser userInfo = (SysUser) params.get("userInfo");
        Map binding = bindingMapper.getBinding(userInfo.getUserAccount());
        if(CollectionUtils.isEmpty(binding)){
            result.getBean().put("isBinding",false);
        }else{
            result.getBean().put("isBinding",true);
        }
        return result;
    }

    class task extends  Thread{
        private Map params;
        public task(Map params) {
            this.params=params;
        }
        @Override
        public void run() {
            //获取当前学期
            Map semester = settingMapper.getSetting("currentSemester");
            String nowSemester = (String) semester.get("settingValue");
            params.put("semester",nowSemester);
            SysUser userInfo = (SysUser) params.get("userInfo");
            //导出数据是、生成文件下载链接
            int[] sizi = {20,20,20,20,20,20,20};
            String[] names = {"学号","姓名","实训单位名","指导老师","指导老师手机号码","校外导师姓名","校外导师手机号码"};
            String[] keys = {"studentAccount","studentName","internshipName","teachName","teachPhone","tutor","tutorPhone"};
            String fileName = "实训信息导出_"+FileNameUtil.getFileName()+".xlsx";
            //创建提醒消息
            Map notify = new HashMap();
            notify.put("sendAccount","admin");
            notify.put("sendType",0);
            notify.put("notifyType",4);
            notify.put("targetAccount",userInfo.getUserAccount());
            notify.put("targetType",0);
            notify.put("creatAccount",userInfo.getUserAccount());
            try {
                List<Map> bindingList = bindingMapper.selectBinding(params);
                if (CollectionUtils.isEmpty(bindingList)) {
                    notify.put("content","没有符合条件的实训信息！导出失败！");
                }else{
                    String fileUrl = ExcelUtils.myExportExcel(names, keys, sizi, fileName, bindingList);
                    notify.put("contentUrl",fileUrl);
                    notify.put("content","你启动的实训信息导出任务已执行完毕，文件下载地址为："+fileUrl);
                }
            }catch (Exception e){
                notify.put("content","你启动的实训信息导出任务执行异常！请稍后再试");
            }
            //发送提醒消息
            notifySV.addNotify(notify);
        }
    }

}
